#!/usr/bin/bash

tel=(
电话号码
)

dir_list=(
send_empty_msg_time
send_msg_time
send_msg_time_0
send_msg_time_1
send_msg_time_meilian
alert_count
empty_alert_count
)

for i in ${dir_list[@]}
do
    touch /tmp/$i
done

mnt1=$(ls -1 /mnt/$(date +%Y%m%d)/ | wc -l)
mnt2=$(ls -1 /mnt2/$(date +%Y%m%d)/ | wc -l)

######### 查看30分钟前的日期文件夹里是否一小时未接收文件##########
empty_mnt1=$(find /mnt/$(date -d "30min ago" +%Y%m%d) -mmin -60 -type f | wc -l)
empty_mnt2=$(find /mnt2/$(date -d "30min ago" +%Y%m%d) -mmin -60 -type f | wc -l)


msg=$(echo "新华社主备存储不一致，登陆10.149.212.96查看/mnt和/mnt2文件数量是否一致" | iconv -f utf8 -t gbk )
empty_msg=$(echo "新华社一小时内无文件写入，登陆10.149.212.96查看/mnt和/mnt2" | iconv -f utf8 -t gbk )

now=$(date +%s)

alert_sum=$(cat /tmp/alert_count)
empty_alert_sum=$(cat /tmp/empty_alert_count)

############################查看上次发送短信时间#############################

if [ -f /tmp/send_msg_time ] 
then 
    last_send=$(cat /tmp/send_msg_time)
else
    last_send=0
fi

################判断同步是否一致，近期未发送且连续两次不一致发送告警 ##########

if (( mnt1 != mnt2 && now-last_send>=1800 )) 
then
    alert_sum=$(( alert_sum + 1 ))
    echo $alert_sum > /tmp/alert_count
fi

if (( alert_sum>=2 )) 
then
    for i in ${tel[@]}
    do
        curl "http://短信网关?OperID=xxxx&OperPass=****&DesMobile=${i}&Content=${msg}&ContentType=8"
    done
    date +%s > /tmp/send_msg_time
    echo 0 > /tmp/alert_count
fi

##################查看另一个问题短信发送时间###################################

if [ -f /tmp/send_empty_msg_time ] 
then 
    last_send_empty=$(cat /tmp/send_empty_msg_time)
else
    last_send_empty=0
fi

#############判断近期是否长期未接收到稿件，近期未告警则发送告警################

if (( (empty_mnt1 == 0 || empty_mnt2 == 0) && now-last_send_empty>=1800 ))
then
    empty_alert_sum=$(( empty_alert_sum + 1 ))
    echo $empty_alert_sum > /tmp/empty_alert_count
fi

if (( empty_alert_sum>=2 ))
then
    for i in ${tel[@]}
    do
        curl "http://短信网关?OperID=xxxx&OperPass=****&DesMobile=${i}&Content=${empty_msg}&ContentType=8"
    done
    date +%s > /tmp/send_empty_msg_time
    echo 0 > /tmp/empty_alert_count
fi

#################################################################
